class Solution
{
public:
    int longestCommonPrefix(vector<int> &arr1, vector<int> &arr2)
    {
        class Trie
        {
        public:
            vector<Trie *> children;

            Trie() : children(10, nullptr) {}
        };

        Trie *root = new Trie();
        for (auto &num : arr1)
        {
            Trie *cur = root;
            string numStr = to_string(num);
            for (auto &c : numStr)
            {
                int digit = c - '0';
                if (cur->children[digit] == nullptr)
                {
                    cur->children[digit] = new Trie();
                }
                cur = cur->children[digit];
            }
        }

        int result = 0;
        for (auto &num : arr2)
        {
            Trie *cur = root;
            string numStr = to_string(num);
            int curLen = 0;
            for (auto &c : numStr)
            {
                int digit = c - '0';
                if (cur->children[digit] == nullptr)
                {
                    break;
                }
                cur = cur->children[digit];
                curLen++;
            }
            result = max(result, curLen);
        }
        return result;
    }
};